package org.hhchat.leetcode.sort;

import java.util.*;

/**
 * Created this one by HMH on 2017/5/27.
 */
public class code349 {
    public static class Solution {
        public int[] intersection(int[] nums1, int[] nums2) {
            if(nums1.length==0 || nums2.length==0) return new int[0];
            Map<Integer, Boolean> nums1Map = getMap(nums1);
            Map<Integer, Boolean> nums2Map = getMap(nums2);
            //check
            List<Integer> result = new ArrayList<>();
            for (Map.Entry<Integer, Boolean> entity : nums1Map.entrySet()) {
                int key = entity.getKey();
                if (nums1Map.get(key)!=null&& nums2Map.get(key)!=null) {
                    result.add(key);
                }
            }
            int[] resultArr = new int[result.size()];
            for(int i=0;i<result.size();i++) {
                resultArr[i] = result.get(i);
            }
            return resultArr;
        }

        public Map<Integer, Boolean> getMap(int[] nums) {
            Map<Integer, Boolean> maps = new HashMap<>();
            for (int now : nums) {
                Boolean isExisted = maps.get(now);
                if(isExisted==null){
                    maps.put(now,true);
                }
            }
            return maps;
        }

        public static void main(String[] args){
            int[] a = new int[]{1,2};
            int[] b = new int[]{1,1};
            Solution solution = new Solution();
            int[] result = solution.intersection(a, b);
            System.out.println(Arrays.toString(result));
        }
    }
}
